package com.sping_boot3.spirng_boot3.juc;

import lombok.Data;
import lombok.SneakyThrows;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@Data
public class 线程定制化化通信 {
    //1 aa  2 bb  3 cc
    private short flag = 1;
    ReentrantLock lock = new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();
    @SneakyThrows
    public void printAA(){
        lock.lock();
        try {
            while (flag != 1){
                condition1.await();
            }
            for (int i = 0; i < 1; i++) {
                System.out.println(Thread.currentThread().getName()+":AA");
            }
            flag = 2;
            condition2.signal();
        } finally {
            lock.unlock();
        }
    }
    @SneakyThrows
    public void printBB(){
        lock.lock();
        try {
            while (flag != 2){
                condition2.await();
            }
            for (int i = 0; i < 2; i++) {
                System.out.println(Thread.currentThread().getName()+":BB");
            }
            flag = 3;
            condition3.signal();
        } finally {
            lock.unlock();
        }
    }
    @SneakyThrows
    public void printCC(){
        lock.lock();
        try {
            while (flag != 3){
                condition3.await();
            }
            for (int i = 0; i < 3; i++) {
                System.out.println(Thread.currentThread().getName()+":CC");
            }
            flag = 1;
            condition1.signal();
        } finally {
            lock.unlock();
        }

    }
}
